<template>
  <div style="padding: 16px;">
    <div
      style="font-size: 16px;font-weight: 600;color: #1D2129;margin-bottom: 16px;"
    >
      体校类别
    </div>
    <div
      style="background: #F3F3F3; width: 100%;height: 40px;line-height: 40px;display: flex;justify-content: space-between;padding: 0 12px;"
    >
      <el-radio-group v-model="activeTab" size="small" @change="datalist">
        <el-radio-button label="1">体校自办项目</el-radio-button>
        <el-radio-button label="2">体校依托办项目</el-radio-button>
      </el-radio-group>
    </div>

    <div id="cc" style="height: 280px;"></div>
  </div>
</template>

<script>
import * as echarts from "echarts";
import { schoolSport } from "@/api/statistics";
export default {
  data() {
    return {
      date: new Date().getFullYear(),
      activeTab: 1
    };
  },

  mounted() {
    this.datalist();
  },
  methods: {
    async datalist(e) {
      const res = await schoolSport({
        year: this.date,
        schoolSportTypeId: e || this.activeTab
      });
      const series = res.data.series;
      const xAxis = res.data.xAxis;
      // 基于准备好的dom，初始化echarts实例
      var myChart = echarts.init(document.getElementById("cc"));
      // 指定图表的配置项和数据
      var option;

      option = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow"
          }
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true
        },
        xAxis: [
          {
            type: "category",
            data: xAxis,
            axisTick: {
              alignWithLabel: true
            }
          }
        ],
        yAxis: [
          {
            type: "value"
          }
        ],
        series: [
          {
            name: "数量",
            type: "bar",
            barWidth: "60%",
            data: series
          }
        ]
      };

      // 使用刚指定的配置项和数据显示图表。
      myChart.setOption(option);

      // 监听父元素大小变化 刷新图表
      var ro = new ResizeObserver(entries => {
        for (let entry of entries) {
          const cr = entry.contentRect;
          myChart.resize();
        }
      });

      ro.observe(cc);
    }
  }
};
</script>

<style></style>
